﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using ImageTagToolViewModel.Others;
using ImageTagToolViewModel.Interfaces;
using System.IO;

namespace ImageTagToolViewModel.ViewModels
{
    /// <summary>
    /// View-Model of the SettingsWindow.
    /// </summary>
    class SettingsViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private IMainWindow _SettingsWindow;

        #region UIBindings
        private bool _RememberLastUsedFolderChecked = true;
        /// <summary>If the Last used folder is checked.</summary>
        public bool RememberLastUsedFolderChecked
        {
            get { return _RememberLastUsedFolderChecked; }
            set
            {
                _RememberLastUsedFolderChecked = value;
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("RememberLastUsedFolderChecked"));
            }
        }

        private bool _ActualFolderChecked = false;
        /// <summary>If the actual folder is enabled.</summary>
        public bool ActualFolderChecked
        {
            get { return _ActualFolderChecked; }
            set
            {
                _ActualFolderChecked = value;
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ActualFolderChecked"));
            }
        }

        private string _ActualFolderPath;
        /// <summary>The specified actual folder path.</summary>
        public string ActualFolderPath
        {
            get { return _ActualFolderPath; }
            private set
            {
                _ActualFolderPath = value;
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ActualFolderPath"));
            }
        }

        private bool _JPG = false;
        /// <summary>If the jpg checkbox is checked.</summary>
        public bool JPG
        {
            get { return _JPG; }
            set
            {
                _JPG = value;
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("JPG"));
            }
        }

        private bool _BMP = false;
        /// <summary>If the bmp checkbox is checked.</summary>
        public bool BMP
        {
            get { return _BMP; }
            set
            {
                _BMP = value;
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("BMP"));
            }
        }

        private bool _PNG = false;
        /// <summary>If the png checkbox is checked.</summary>
        public bool PNG
        {
            get { return _PNG; }
            set
            {
                _PNG = value;
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("PNG"));
            }
        }

        private int _MarginTop;
        /// <summary>The top margin of print page.</summary>
        public int MarginTop
        {
            get { return _MarginTop; }
            private set
            {
                _MarginTop = value;
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MarginTop"));
            }
        }

        private int _MarginRight;
        /// <summary>The right margin of print page.</summary>
        public int MarginRight
        {
            get { return _MarginRight; }
            private set
            {
                _MarginRight = value;
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MarginRight"));
            }
        }

        private int _MarginBottom;
        /// <summary>The bottom margin of print page.</summary>
        public int MarginBottom
        {
            get { return _MarginBottom; }
            private set
            {
                _MarginBottom = value;
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MarginBottom"));
            }
        }

        private int _MarginLeft;
        /// <summary>The left margin of print page.</summary>
        public int MarginLeft
        {
            get { return _MarginLeft; }
            private set
            {
                _MarginLeft = value;
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MarginLeft"));
            }
        }

        private bool _AllowUpdate = false;
        /// <summary>If the update checkbox is checked.</summary>
        public bool AllowUpdate
        {
            get { return _AllowUpdate; }
            set
            {
                _AllowUpdate = value;
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AllowUpdate"));
            }
        }
        #endregion UIBindings

        #region Actions
        public DelegateCommand CancelButtonClicked { get; private set; }
        public DelegateCommand OkButtonClicked { get; private set; }
        #endregion Actions

        public SettingsViewModel(IMainWindow settingsWindow)
        {
            _SettingsWindow = settingsWindow;
            SetUpValues();
            SetUpCommands();
        }
        
        /// <summary>
        /// Enter the values using the SettingsProvider
        /// </summary>
        private void SetUpValues()
        {
            SettingsProvider sp = new SettingsProvider();
            ActualFolderPath = sp.getDefaultFolder();
            string[] formats = sp.getAvailableFormats();
            if (formats.Contains("JPG"))
            {
                JPG = true;
            }
            if (formats.Contains("BMP"))
            {
                BMP = true;
            }
            if (formats.Contains("PNG"))
            {
                PNG = true;
            }
            AllowUpdate = sp.getAllowUpdate();
            int[] margins = sp.getPrintMargins();
            if (margins.Count() == 4)
            {
                MarginTop = margins[0];
                MarginRight = margins[1];
                MarginBottom = margins[2];
                MarginLeft = margins[3];
            }
        }

        private void SaveAllValues()
        {
            SettingsProvider sp = new SettingsProvider();
            sp.setDefaultFolder(ActualFolderPath);
            string[] formats = new string[3];
            int i = 0;
            if (JPG)
            {
                formats[i] = "JPG";
                i++;
            }
            if (BMP)
            {
                formats[i] = "BMP";
                i++;
            }
            if (PNG)
            {
                formats[i] = "PNG";
                i++;
            }
            sp.setAvailableFormats(formats);
            sp.setAllowUpdate(AllowUpdate);
            int[] margins = new int[4];
            margins[0] = MarginTop;
            margins[1] = MarginRight;
            margins[2] = MarginBottom;
            margins[3] = MarginLeft;
            sp.setPrintMargins(margins);
        }

        private void SetUpCommands()
        {
            CancelButtonClicked = new DelegateCommand((o) =>
            {
                // Closing the settings window.
                _SettingsWindow.CloseWindow();
            });
            OkButtonClicked = new DelegateCommand((o) =>
            {
                //save all settings
                SaveAllValues();
                // Closing the settints window
                _SettingsWindow.CloseWindow();
            });
        }

    }
}
